package com.smart.four.ioc.beanfactory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * 测试BeanFactory
 *
 * @author junrui.wang
 * @create 2018/4/24
 */
public class BeanFactory1 {

    public static void getBean() throws IOException {
        ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
        Resource res = rpr.getResource("classpath:com/smart/four/ioc/factorybean/beans.xml");
        System.out.println(res.getURL());

        //被废弃，不建议使用
        //BeanFactory factory = new XmlBeanFactory();

        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(res);
        System.out.println("init BeanFactory");

        Car car = factory.getBean("Car", Car.class);
        System.out.println("Car bean is ready for use");
        car.introduce();
    }
}
